import { defineStore } from 'pinia'
export const useCartStore = defineStore({
  id: 'cart',
  state: () => {
    return {
      cartList:[],
      select:[],
    }
  },
  getters:{
    isChecked(){
      return this.select.length == this.cartList.length;
    }
  },
  actions:{
   addCart(list){
    this.list.forEach(v => {
      v['check'] = true;
      this.select.push(v.id)
    })
    this.cartList = list
   },
   all(){
    this.select = this.cartList.map((v)=>{
      v['check'] = true;
      return v.id
    })
   },
   unAll(){
    this.cartList.forEach(v => {
      v['check'] = false;
    })
    this.select = []
   },
   itemChecked(index){
    let id = this.cartList[index].id;
    let idx = this.select.indexOf(id);
    if(idx > -1){
      this.cartList[index].check = false;
      this.select.splice(idex,1)
    }
   }
  }
})